<?php


namespace app\system\service;

class ApiCodeService
{
	public static function getlist($options)
	{
		$where = [];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["type"]) && $options["type"] != "") {
			$where[] = ["type", "=", $options["type"]];
		}
		if (isset($options["api_id"]) && $options["api_id"] != "") {
			$where[] = ["api_id", "=", $options["api_id"]];
		}
		if (isset($options["message"]) && $options["message"] != "") {
			$where[] = ["", "exp", \think\Db::raw("`message` like '%{$options["message"]}%'")];
		}
		$list = \think\Db::name("app_api_code")->where($where)->order(["id" => "asc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_api_code")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_array_info($data)
	{
		$data["api_info"] = \think\Db::name("app_api")->where(["id" => $data["api_id"]])->field("id,name")->find() ?? (object) [];
		return $data;
	}
	public static function getinfo($options)
	{
		$item = \think\Db::name("app_api_code")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => self::get_array_info($item)];
	}
	public static function add($options)
	{
		\think\Db::startTrans();
		try {
			\think\Db::name("app_api_code")->insert(["type" => $options["type"], "api_id" => $options["api_id"], "message" => $options["message"]]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_api_code")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$data = ["type" => $options["type"], "api_id" => $options["api_id"], "message" => $options["message"]];
			\think\Db::name("app_api_code")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_api_code")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_api_code")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_app_config($options)
	{
		$row = \think\Db::name("app_api_code")->where(["id" => $options["code_id"]])->find();
		if (empty($row)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "code_id不存在"];
		}
		$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
		if (empty($app)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
		}
		$api_code_config = json_decode($app["api_code_config"], true);
		$config = ["id" => "", "message" => ""];
		if (isset($api_code_config[$row["id"]])) {
			$config = $api_code_config[$row["id"]];
		}
		return ["code" => \app\system\model\Code::success, "data" => $config];
	}
	public static function set_app_config($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_api_code")->where(["id" => $options["code_id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "code_id不存在"];
			}
			$app = \think\Db::name("app")->where(["id" => $options["appid"]])->find();
			if (empty($app)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "appid不存在"];
			}
			$api_code_config = json_decode($app["api_code_config"], true);
			$api_code_config[$row["id"]] = ["id" => $options["id"] ?? "", "message" => $options["message"] ?? ""];
			\think\Db::name("app")->where("id", $app["id"])->update(["api_code_config" => json_encode($api_code_config, JSON_UNESCAPED_UNICODE)]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}